
using QTool.BEPUphysics.CollisionShapes;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace QTool.NetFrame
{
    [RequireComponent(typeof(Terrain))]
    public class FixTerrainCollider:QFixBehaviour
    {
        public Terrain terrain;
        [Range(2, 20)]
        public int resolutionScaleDiv = 10;
        private void Reset()
        {
            terrain = GetComponent<Terrain>();
        }
    
        private QTool.BEPUphysics.BroadPhaseEntries.Terrain fixTerrain;
        public override void OnFixAwake()
        {
            base.OnFixAwake();
            FixPhysics.space.Add(fixTerrain);
        }
        private void Awake()
        {
            var resolusion = terrain.terrainData.heightmapResolution;
            var heights = terrain.terrainData.GetHeights(0,0, resolusion, resolusion);
            var size = new FixVector3(
               terrain.terrainData.size.x / (resolusion/ resolutionScaleDiv - 1)
               , 1,
               terrain.terrainData.size.x / (resolusion/ resolutionScaleDiv - 1)
               );
            var fixHeights = new Fix64[resolusion/ resolutionScaleDiv, resolusion/ resolutionScaleDiv];
            for (int i = 0; i < fixHeights.GetLength(0); i++)
            {
                for (int j = 0; j < fixHeights.GetLength(1); j++)
                {
                    fixHeights[i, j] =  (Fix64)heights[j* resolutionScaleDiv, i* resolutionScaleDiv] * terrain.terrainData.size.y;
                }
            }
            fixTerrain = new QTool.BEPUphysics.BroadPhaseEntries.Terrain(fixHeights, 
                new AffineTransform(size, transform.rotation, transform.position));
          
          
            //  t.thickness = height;
        }
        public override void OnFixDestroy()
        {
            base.OnFixDestroy();
            FixPhysics.space.Remove(fixTerrain);
        }
        //protected override EntityShape CreateShape()
        //{
        //    var mesh=new TerrainShape
        //}
        private void OnDrawGizmosSelected()
        {
            if (fixTerrain == null) return;
            Gizmos.color = Color.Lerp(Color.blue, Color.clear, 0.5f);

            var resolusion = terrain.terrainData.heightmapResolution;
            var size = new FixVector3(
               terrain.terrainData.size.x/(resolusion/ resolutionScaleDiv - 1)
               , 1,
               terrain.terrainData.size.x/(resolusion/ resolutionScaleDiv - 1)
               );
            Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, ((Vector3)size));
            for (int i = 0; i < fixTerrain.Shape.Heights.GetLength(0); i+=1)
            {
                for (int j = 0; j < fixTerrain.Shape.Heights.GetLength(1); j+=1)
                {
                    Gizmos.DrawCube(new Vector3(i, ((float)fixTerrain.Shape.Heights[i,j]), j), Vector3.one);
                }
            }
        }

     
    }
}
